<template>
    <div class="common">
        <innerCommon 
            :location="location"
        >
            <template slot="left">
                <span>{{type==0?'新增':'修改'}}</span>
            </template>
            <div slot="right">
                <a-button type="primary" @click="returnBack()">返回</a-button>
            </div>
            <div slot="table">
                <eform
                    v-if="fields||type==0"
                    :formData="formData"
                    class="form"
                    ref="form"
                    :fields="fields"
                >
                    <div slot="content">
                        <eQuill
                            @getQuillContent="getQuillContent"
                            v-decorator="['content',{rules: [{ required: true, message: '请输入内容' }]}]"
                            :defaultData="quillInfo"
                        ></eQuill>
                    </div>
                    <div slot="attachFile">
                        <attachFile
                            v-decorator="['attachFile']"
                            :fileList="fileList"
                            @getFileUrl="getFileUrl"
                        ></attachFile>
                    </div>
                </eform>
                <div class="quillInfoText" v-html="quillInfo"></div>
                <div class="submitBtn">
                    <a-button @click="returnBack()">取消</a-button>
                    <a-button type="primary" @click="save()">保存</a-button>
                </div>
            </div>
        </innerCommon>
    </div>
</template>
<script>
import eform from "@/components/common/form"
import eQuill from "@/components/common/eQuill"
import innerCommon from "@/components/common/innerCommon"
import attachFile from "@/components/common/attachFile"
import {addAllianceCooperationApi,getCooperationDetailApi,editAllianceCooperationApi} from "@/request/api"
export default {
    name:'allianceCooperationAdd',
    components:{innerCommon,eform,eQuill,attachFile},
    created(){
        this.type=this.$route.query.id?1:0 
        if(this.type==1){
            this.newsId=this.$route.query.id
            this.getToken(getCooperationDetailApi,{'newsId':this.newsId},res=>{
                this.fields=res.data
                this.quillInfo=res.data.content
                this.fileList=res.data.attachFile
            })
        }
        this.location=[{
            name:"信息发布管理",
            path:''
        },{
            name:"资讯中心",
            path:'/news'
        },{
            name:this.type==0?'新增':'修改',
            path:'/newsAdd'
        }]
        this.init()
    },
    data(){
        const formData=[
            {
                label:"标题",
                prop:"title",
                placeholder:'请输入标题',
                rules:[{required:true,message:'请输入标题'}],
            },{
                label:"内容",
                scopedSlots: { customRender: 'content'},
            },{
                label:"附件",
                scopedSlots: { customRender: 'attachFile'},
            }
        ]
        return{
            formData,
            location:[],
            type:0,//0新增 1修改
            fields:null,
            quillInfo:'',
            newsId:'',
            fileList:[]
        }
    },
    methods:{
        init(){

        },
        returnBack(){
            this.$confirm({
                title: '确认返回吗?',
                cancelText:"取消",
                okText:'确定',
                onOk:()=>{
                    this.$router.go(-1)
                },
                onCancel() {
                },
            });
        },
        getQuillContent(data){
            this.quillInfo=data
            this.$refs.form.form.setFieldsValue({['content']:data})
        },
        save(){
            this.$refs.form.form.validateFields().then(res=>{
                let formData={...res,'info':this.getContent().replace(/\s*/g,"")}
                let api=''
                if(this.type==0){
                    api=addAllianceCooperationApi
                }else{
                    api=editAllianceCooperationApi
                    formData={...formData,'newsId':this.newsId}
                }
                this.getToken(api,formData,data=>{
                    this.$message.success(data.message)
                    this.$router.go(-1)
                })
            })
        },
        getContent(){
            return document.getElementsByClassName('quillInfoText')[0].innerText
        },
        getFileUrl(data){
            this.$refs.form.form.setFieldsValue({['attachFile']:data})
        },
    }
}
</script>